<?php
add_mini_help('foresee');

MainDesign::leftSubmenuBeginning("Rodyti");
MainDesign::viewElementsList();
MainDesign::leftSubmenuEnding();

MainDesign::h1("Pakeitimų įtaka");


/* Original sudivisions data */
require 'Processing/Subdivisions/subdivision_data_processing.php';
require 'DB/db_data_loader.php';
$subdivisions = get_subdivisios_associative();
$measures = get_support_measures_associative();
$dates = SubdivisionData::get_dates_predicted(12);
$dataset = SubdivisionData::counts_to_dataset();

$measures_subdivisions = SubdivisionData::get_measures_subdivisions();
$time_groups = SubdivisionData::dataset_to_table($dataset, $subdivisions, $dates, $is_time_groups, $measures_subdivisions);
SubdivisionData::predict($time_groups);


/* Original information systems data */
require 'Processing/InformationSystems/is_data_processing.php';
$is_names = get_is_associative();
ISData::predict_is($is_time_groups);


/* Input */
if ($_POST) {
    $new_measures = $_POST['new_measure'];
    $measures2 = $measures + $new_measures;

    $date_keys = array_keys($dates);
    $index = count($dataset);
    $predicted_dates = 12;
    foreach ($new_measures as $measure=>$name) {
        /* Empty dates */
        foreach($date_keys as $date) {
            if (!$dates[$date]) {
                $new_dataset[$index] = array($date, $measure, 0, 0);
                $index++;
            }
        }
        
        /* Manually predicted dates */
        foreach ($_POST['received'][$measure] as $date=>$received) {
            $administrated = $_POST['received'][$measure][$date];
            if (is_numeric($administrated) && is_numeric($received)) {
                $new_dataset[$index] = array($date, $measure, $received, $administrated);
            } else {
                $new_dataset[$index] = array($date, $measure, 0, 0);
            }
            $index++;
        }
    }
    $dataset2 = $dataset + $new_dataset;

    $random = 0;
    foreach ($subdivisions as $subdivision=>$value) {
        foreach ($new_measures as $measure=>$name) {
            if ($_POST['subdivision'][$measure][$subdivision] && is_numeric($_POST['subdivision'][$measure][$subdivision])) {
                $new_measures_subdivisions[$measure][$subdivision] = $_POST['subdivision'][$measure][$subdivision];
            } else {
                $new_measures_subdivisions[$measure][$subdivision] = 0;
            }
        }
        $random++;
    }
    $measures_subdivisions2 = $measures_subdivisions + $new_measures_subdivisions;

    $time_groups2 = SubdivisionData::dataset_to_table($dataset2, $subdivisions, $dates, $is_time_groups2, $measures_subdivisions2);
    SubdivisionData::predict($time_groups2);
    ISData::predict_is($is_time_groups2);
}

/* New elements */
MainDesign::viewSectionBegin("Naujos paramos priemonės pridėjimas");
require 'ForeseeDesign.php';
ForeseeDesign::show_input($subdivisions, SubdivisionData::get_predicted_dates($dates), $_POST);
MainDesign::viewSectionEnd();

/* Graphics and tables */
if ($_POST) {
    require 'GUI/Tabs/LoadView/LoadViewDesign.php';
    MainDesign::viewSectionBegin('Dabartinė padalinių diagrama');
        LoadViewDesign::linear_sub2($dates, $subdivisions, $time_groups, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin("Numatoma padalinių diagrama");
        LoadViewDesign::linear_sub2($dates, $subdivisions, $time_groups2, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Dabartinė padalinių lentelė');
        LoadViewDesign::table_sub($dates, $subdivisions, $time_groups, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Numatoma padalinių lentelė');
        LoadViewDesign::table_sub($dates, $subdivisions, $time_groups2, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Dabartinė IS lentelė');
        LoadViewDesign::table_is($dates, $is_names, $is_time_groups, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Numatoma IS lentelė');
        LoadViewDesign::table_is($dates, $is_names, $is_time_groups2, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    $predicted_start = count($dates) - 12;
    $dates = array_slice($dates, $predicted_start);
    foreach ($time_groups2 as $subdivision=>$row) {
        $time_groups3[$subdivision] = array_slice($time_groups[$subdivision], $predicted_start);
        $time_groups4[$subdivision] = array_slice($row, $predicted_start);
    }
    foreach ($is_time_groups2 as $is=>$row) {
        $is_time_groups3[$is] = array_slice($is_time_groups[$is], $predicted_start);
        $is_time_groups4[$is] = array_slice($row, $predicted_start);
    }

    MainDesign::viewSectionBegin('Palyginamoji padalinių lentelė');
        LoadViewDesign::table_compare_sub($dates, $subdivisions, $time_groups3, $time_groups4);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Palyginamoji padalinių kiekių diagrama');
        LoadViewDesign::linear_compare($dates, $subdivisions, $time_groups3, $time_groups4, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Palyginamoji padalinių laiko diagrama');
        LoadViewDesign::linear_compare($dates, $subdivisions, $time_groups3, $time_groups4, LoadViewDesign::DIAGRAM_MODE_TIME);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Palyginamoji informacinių sistemų lentelė');
        LoadViewDesign::table_compare_is($dates, $is_names, $is_time_groups3, $is_time_groups4);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Palyginamoji IS kiekių diagrama');
        LoadViewDesign::linear_compare($dates, $is_names, $is_time_groups3, $is_time_groups4, LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
    MainDesign::viewSectionEnd();

    MainDesign::viewSectionBegin('Palyginamoji IS laiko diagrama');
        LoadViewDesign::linear_compare($dates, $is_names, $is_time_groups3, $is_time_groups4, LoadViewDesign::DIAGRAM_MODE_TIME);
    MainDesign::viewSectionEnd();
}
?>
